package nio.zero_copy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class NIOCopyServer {
    public static void main(String[] args) throws IOException {
        InetSocketAddress address = new InetSocketAddress(9002);
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(address);
        // 创建 buffer
        ByteBuffer buffer = ByteBuffer.allocate(4096);
        while (true) {
            try {
                SocketChannel socketChannel = serverSocketChannel.accept();
                int readCount = 0;
                while (readCount != -1) {
                    readCount = socketChannel.read(buffer);
                    // 倒带
                    buffer.rewind();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
